﻿/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package  envisage.Controls
{
	import envisage.Data.IRange;
	import envisage.Data.UIRangeEvent;
	import envisage.Managers.DragManager;
	import envisage.Controls.Orientation;
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	public class Slider extends Control
	{
		public function Slider()
		{
			orient = Orientation.HORZ;			

			super();
			
		 	RequireLayout();
		}
		
		private var iRange:IRange;
		public function get DataRange():IRange { return iRange; }
		public function set DataRange(value:IRange):void
		{
			if( iRange != null )
			{
				iRange.removeEventListener(UIRangeEvent.UPDATED_A, OnUpdatedA);
				iRange.removeEventListener(UIRangeEvent.UPDATED_B, OnUpdatedB);
				iRange.removeEventListener(UIRangeEvent.UPDATED_POS, OnUpdatedPos);
			}
			
			iRange = value;
			
			if( iRange != null )
			{
				iRange.addEventListener(UIRangeEvent.UPDATED_A, OnUpdatedA);
				iRange.addEventListener(UIRangeEvent.UPDATED_B, OnUpdatedB);
				iRange.addEventListener(UIRangeEvent.UPDATED_POS, OnUpdatedPos);
			}
			
			RequireLayout();
		}
		
		protected var thumb:DisplayObject = null;
		public function get Thumb():DisplayObject { return thumb; }
		public function set Thumb(value:DisplayObject):void
		{
			this.thumb = value; RequireLayout();
			DragManager.AllowDrag(thumb, true);
			thumb.addEventListener(DragEvent.DRAG_START,  OnThumbDragStart);
			thumb.addEventListener(DragEvent.DRAG_UPDATE, OnThumbDragUpdate);
		}
		
		private var pointA:Point = new Point();
		public function set PointA(value:Point):void
		{
			if( value.x != pointA.x || value.y != pointA.y )
			{
				pointA.x = value.x;
				pointA.y = value.y;
				RequireLayout();
			}
		}
		
		private var length:Number = 0;
		public function set Length(value:Number):void
		{
			if( length != value )
			{
				length = value;
				RequireLayout();
			}
		}
		
		protected var orient:String;
		public function get Orient():String { return orient; }
		public function set Orient(orient:String):void { this.orient = orient; RequireLayout(); }
		
		private var thumbPoint:Point = new Point(0,0);
		public function set ThumbPoint(value:Point):void { this.thumbPoint = value; RequireLayout(); }
		
		private var startPos:Number;
		private function OnThumbDragStart(e:DragEvent):void
		{
			if( iRange == null )return;
			startPos = iRange.GetPos();
		}
		
		private function OnThumbDragUpdate(e:DragEvent):void
		{
			if( iRange == null )return;
			
			var amtPerPixel:Number = (iRange.GetB() - iRange.GetA()) / length;
			
			if( orient == Orientation.HORZ ) SetPos(startPos + (amtPerPixel * e.totalX));
			else						SetPos(startPos + (amtPerPixel * e.totalY));
		}

		//Set the position of the slider.
		public function SetPos(p:Number):void
		{
			if( iRange == null ) return;
			
			if( p > iRange.GetB() ) p = iRange.GetB();
			if( p < iRange.GetA() ) p = iRange.GetA();
			
			if( p != iRange.GetPos() ) 
			{
				iRange.SetPos(p);
				RequireLayout();
				//dispatchEvent(new UIEvent(UIEvent.SLIDER_UPDATE, iRange.GetPos()));
				dispatchEvent(new EnvisageEvent(EnvisageEvent.UPDATED, iRange.GetPos(), this, null));
			}
		}
		
		/*
		private function OnThumbDoubleClick(e:MouseEvent):void
		{
			dispatchEvent(new UIEvent(UIEvent.SLIDER_DOUBLE_CLICK_THUMB)); 
		}*/
		
		protected override function Layout():void
		{
			var iRange:IRange = DataRange;
			if( iRange == null ) return;
			
			if( thumb == null ) return;
			
			var fac:Number = (iRange.GetPos()-iRange.GetA())/(iRange.GetB()-iRange.GetA());
			
			if( orient == Orientation.HORZ )
			{
				thumb.x = pointA.x + (length) * fac;
				thumb.y = pointA.y;
			}
			
			else
			{
				thumb.x = pointA.x;
				thumb.y = pointA.y + (length) *fac;
			}
			
			thumb.x -= thumbPoint.x;
			thumb.y -= thumbPoint.y;
		}
		
		private function OnUpdatedA(e:UIRangeEvent):void
		{
			SetPos(iRange.GetPos());
			RequireLayout();
		}
		
		private function OnUpdatedB(e:UIRangeEvent):void
		{
			SetPos(iRange.GetPos());
			RequireLayout();
		}
		
		private function OnUpdatedPos(e:UIRangeEvent):void
		{
			SetPos(iRange.GetPos());
			RequireLayout();	
		}
	
	}
}



		



